package eteam.aps.service.impl;

import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.SalesAuditMapper;
import eteam.aps.model.dto.SalesAuditDto;
import eteam.aps.model.entity.SalesAudit;
import eteam.aps.service.SalesAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


/**
 * SalesAudit ServiceImpl 实现类
 *
 * @author ll
 * @since 2023-06-02
 */
@Service
public class SalesAuditServiceImpl implements SalesAuditService {

    @Autowired
    private SalesAuditMapper salesAuditMapper;

    @Override
    @Transactional
    public int updateSalesAudit(SalesAudit salesAudit) {
        return salesAuditMapper.updateSalesAudit(salesAudit);
    }

    @Override
    public JsonPage<SalesAudit> selectSalesAuditList(SalesAuditDto salesAuditDto) {
        Map<String, Object> map = new HashMap<>(7);
        map.put("state", ParentEntity.FSTATE_DELETED);
        map.put("fSalesOrder", salesAuditDto.getfSalesOrder());
        map.put("fOrderNum", salesAuditDto.getfOrderNum());
        // 审核状态
        if (salesAuditDto.getAuditState() == 0) map.put("audit", "no");
        map.put("startTime", salesAuditDto.getEndTime());
        map.put("endTime", salesAuditDto.getEndTime());
        long count = salesAuditMapper.selectSalesAuditCount(map);
        int page = salesAuditDto.getPage();
        int size = salesAuditDto.getSize();
        if (count == 0) return JsonPage.builder(page, size, (int) count, Collections.emptyList());
        map.put("offSize", salesAuditDto.offSize());
        map.put("pageSize", size);
        return JsonPage.builder(page, size, (int) count, salesAuditMapper.selectSalesAuditList(map));
    }

    @Override
    public SalesAudit selectOneSalesAudit(String fId) {
        return salesAuditMapper.selectOneSalesAudit(fId);
    }

}
